<?
$q = "
	SELECT photo.*, mark.mark
	FROM photo
	LEFT JOIN mark ON mark.id_photo = photo.id_photo
	LIMIT 0, 50
";

$result = mysql_query($q);
$ph = array();
$marks = array();
$photos = array();
while($row = mysql_fetch_assoc($result)){
	$ph[$row['id_photo']] = $row['photo'];
	$marks[$row['id_photo']][] = $row['mark'];
}

foreach($marks as $id => $mark){
	$mark = array_sum($mark) / count($mark);
	$photos[$mark] = $ph[$id];
}

foreach($photos as $mark => $photo){
	echo "<img src='$photo'/>";
}